package com.qqq.mstemplateservice.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.qqq.commoncache.util.CacheUtil;
import com.qqq.commonredis.service.RedisService;
import com.qqq.mstemplateservice.service.IClothService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.github.benmanes.caffeine.cache.Cache;

import java.util.concurrent.TimeUnit;

@Service
public class ClothserviceImpl implements IClothService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private Cache<String, Object> cache;

    @Override
    public Integer clothPriceGet(long proId) {
        String key = "c:" + proId;
        Integer price = CacheUtil.getL2Cache(redisService, key,new TypeReference<Integer>() {},cache);
        if(price != null){
            return price;
        }
        price = getPriceFromDB(proId);
        return price;
    }

    private Integer getPriceFromDB(long proId) {
        //将查出来的数据分别放入二级缓存和一级缓存中
        Integer price = 100;//通过sql从数据库中查出指定商品在一年之间的平静售卖价格
        String key = "c:" + proId;
        CacheUtil.setL2Cache(redisService,key,price,cache,120L, TimeUnit.SECONDS);
        return price;

    }
}
